package v2

import (
	"fmt"
	"strings"
)

func Worker(r Request) (ParseResult,error) {

	body,err := Fetcher(r.Url)
	if err != nil {
		fmt.Printf("请求[%s]失败：%s",r.Url,err)
		return ParseResult{},err
	}

	return r.Parse.Parser(body,r.Url),nil
}



type SimpleEngine struct {}

func (e *SimpleEngine) Run(queue ...Request) {
	var count = 0
	for len(queue) > 0 {
		r := queue[0]
		queue = queue[1:]

		results,err := Worker(r)
		if err != nil {
			return
		}
		for _,r := range results.Requests {
			if strings.Index(r.Url,"http") == -1{
				r.Url = "http://m.mamicode.com" + r.Url
			}
			if IsDuplicate(r.Url) {
				continue
			}
			//fmt.Println("cacheUrlMP length:" , len(cacheUrlMP))
			queue = append(queue,r)
		}
		for _,item := range results.Items {
			count++
			fmt.Printf("Got Item:$%d %v",count,item)
		}
	}
}

var cacheUrlMP = make(map[string]bool)

func IsDuplicate(url string)  bool {
	if cacheUrlMP[url] {
		return true
	}
	cacheUrlMP[url] = true
	return  false
}
